<?php

namespace App\System\Utils;


use Hyperf\Config\Annotation\Value;
use InvalidArgumentException;
use Qbhy\HyperfAuth\Authenticatable;
use Qbhy\HyperfAuth\AuthGuard;
use Qbhy\HyperfAuth\AuthManager;
use Qbhy\SimpleJwt\JWTManager;
use function Hyperf\Support\make;

class AuthUser
{
    protected AuthManager $auth;
    protected JWTManager $jwt;

    protected AuthGuard $guard;

    #[Value("auth.default")]
    protected array $configAuthDefault;

    public function __construct(string $scene = null)
    {
        if (empty($scene)) {
            $scene = $this->configAuthDefault['guard'] ?? 'jwt';
        }
        $this->auth = make(AuthManager::class);
        $this->guard = $this->auth->guard($scene);
        /** @var JWTManager $jwt */
        $this->jwt = $this->guard->getJwtManager();
    }

    /**
     * 验证token.
     */
    public function check(?string $token = null): bool
    {
        if ($this->jwt->parse($token)) {
            return true;
        }
        return false;
    }

    /**
     * 验证密码
     * @param string $password 验证的密码
     * @param string $signaturePassword 加密后的密码
     * @return bool
     */

    public function checkPassword(string $password, string $signaturePassword): bool
    {
        return $this->jwt->getEncrypter()->check($password, $signaturePassword);
    }
    /**
     * 获取JWT对象
     */
    public function getJwt(): JWTManager
    {
        return $this->jwt;
    }

    /**
     * 获取当前登录用户信息.
     */
    public function getUserInfo($id)
    {
        $user = $this->guard->user()::retrieveById($id);
        //判断用户是否为空，不为空转数组
        if (!empty($user)) {
            return $user->toArray();
        }
        return [];
    }

    /**
     * 获取当前登录用户ID.
     */
    public function getId(): int
    {
        return $this->guard->user()->getId();
    }

    /**
     * 获取当前登录用户名.
     */
    public function getUsername(): string
    {
        $userInfo = $this->getUserInfo($this->getId());
        return $userInfo['username'];
    }

    /**
     * 获取当前登录用户角色.
     */
    public function getUserRole(array $columns = ['id', 'name', 'code']): array
    {
        $userInfo = $this->getUserInfo($this->getId());
        //判断用户是否拥有角色
        if (!empty($userInfo['roles'])) {
            return explode(',', $userInfo['roles']);
        }
        return [];
    }

    /**
     * 获取当前登录用户岗位.
     */
    public function getUserPost(array $columns = ['id', 'name', 'code']): array
    {
        $userInfo = $this->getUserInfo($this->getId());
        //判断用户是否拥有角色
        if (!empty($userInfo['post'])) {
            return explode(',', $userInfo['post']);
        }
        return [];
    }

    /**
     * 获取当前登录用户部门.
     */
    public function getUserDept(array $columns = ['id', 'name']): array
    {
        $userInfo = $this->getUserInfo($this->getId());
        //判断用户是否拥有角色
        if (!empty($userInfo['dept'])) {
            return explode(',', $userInfo['dept']);
        }
        return [];
    }

    /**
     * 获取当前token用户类型.
     */
    public function getUserType(): string
    {
        $userInfo = $this->getUserInfo($this->getId());
        return $userInfo['user_type'];
    }s

    /**
     * 是否为超级管理员（创始人），用户禁用对创始人没用.
     */
    public function isSuperAdmin(): bool
    {

        $user = $this->guard->user()::retrieveById($this->getId());

        //判断is_super是否存在，并且是否为1
        return $user->is_super ?? false;
    }


    /**
     * 获取Token.
     * @throws InvalidArgumentException
     */
    public function getToken(Authenticatable $user): string
    {
        return $this->guard->login($user);
    }

    /**
     * 刷新token.
     */
    public function refresh(): string
    {
        return $this->guard->refresh();
    }
}